package com.googlecode.stil;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

public class StilActivity extends BaseGameActivity {

	private static final int CAMERA_WIDTH = 720;
	private static final int CAMERA_HEIGHT = 480;

	private Camera camera;
	private BitmapTextureAtlas textureAtlas;
	private TextureRegion faceTextureRegion;

	/**
	 * Called when the activity is first created. Die moet uitgehaal word om
	 * andengine actually te gebruik anders laai hy net die main "hello" screen
	 * 
	 */
	// @Override
	// public void onCreate(Bundle savedInstanceState) {
	// super.onCreate(savedInstanceState);
	// setContentView(R.layout.main);
	// }

	@Override
	public Engine onLoadEngine() {
		camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera));
	}

	@Override
	public void onLoadResources() {
		textureAtlas = new BitmapTextureAtlas(32, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		faceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(textureAtlas, this, "gfx/face_box.png", 0, 0);

		this.mEngine.getTextureManager().loadTexture(textureAtlas);
	}

	@Override
	public Scene onLoadScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());

		Scene scene = new Scene();
		scene.setBackground(new ColorBackground(0.5f, 0.3f, 0.5f));

		final int centerX = (CAMERA_WIDTH - faceTextureRegion.getWidth()) / 2;
		final int centerY = (CAMERA_HEIGHT - faceTextureRegion.getHeight()) / 2;

		final Sprite face = new Sprite(centerX, centerY, faceTextureRegion) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if (pSceneTouchEvent.isActionUp()) {
					// as jy die screen los dan val hy vloer toe
					this.setPosition(pSceneTouchEvent.getX() - (getWidth() / 2), CAMERA_HEIGHT - getHeight());
				} else {
					this.setPosition(pSceneTouchEvent.getX() - (getWidth() / 2), pSceneTouchEvent.getY() - (getHeight() / 2));
				}
				return true;
			}
		};
		scene.attachChild(face);
		// maak die sprite touchable op die scene
		scene.registerTouchArea(face);
		// maak dat jou vinger nie vir die ding kan weg hardloop en die touch
		// reference verloor nie
		scene.setTouchAreaBindingEnabled(true);

		return scene;
	}

	@Override
	public void onLoadComplete() {

	}

}